let http = require("http");
let mysql = require("mysql");
let fs = require("fs");

http.createServer(function (req, res) {
    if (req.url == '/html/china') {
        res.setHeader('Content-Type', 'text/html;charset=utf-8');
        let connection = mysql.createConnection({
            host: 'localhost',
            port: '3306',
            user: 'root',
            password: '123456',
            database: 'yiqing',
        })
        connection.connect();
        connection.query("select * from china where `date`=?", '2022-03-10', function (err, rows) {
            let html = '';
            for (let row in rows) {
                html += '<tr>'
                html += '<td>' + rows[row].city + '</td>'
                html += '<td>' + rows[row].nums + '</td>'
                html += '<td>' + rows[row].date + '</td>'
                html += '</tr>'
            }
            console.log(html);
            fs.readFile('./html/china.html', function(err, buffer) {
                let outPutStr = buffer.toString();
                let newStr = outPutStr.replace('{{result}}', html);
                res.write(newStr);
                res.end();
            })
        });
    } else {
        res.end();
    }
}).listen(8080);